﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace PhilWicklund.com.FacebookParts
{
    public class LoginPart: System.Web.UI.WebControls.WebParts.WebPart
    {
        public enum Sizes
        {
            small,
            medium,
            large
        };

        private Sizes buttonSize = Sizes.medium;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Button Size"),
         System.Web.UI.WebControls.WebParts.WebDescription("Change the size of the login button."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(Sizes.medium)
        ]
        public Sizes ButtonSize
        {
            get
            {
                return buttonSize;
            }
            set
            {
                buttonSize = value;
            }
        }

        public enum Lengths
        {
            Shorter,
            Longer
        };

        private Lengths buttonLength = Lengths.Shorter;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Button Length"),
         System.Web.UI.WebControls.WebParts.WebDescription("Long = Include 'connect with facebook' text)."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(Lengths.Shorter)
        ]
        public Lengths ButtonLength
        {
            get
            {
                return buttonLength;
            }
            set
            {
                buttonLength = value;
            }
        }

        private bool autologoutlink = true;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Show Logout Link"),
         System.Web.UI.WebControls.WebParts.WebDescription("After someone has logged in, it will show a logout link if true."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(true)
        ]
        public bool AutoLogoutLink
        {
            get
            {
                return autologoutlink;
            }
            set
            {
                autologoutlink = value;
            }
        }

        private Orientations orientation = Orientations.left;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(false),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Orientation"),
         System.Web.UI.WebControls.WebParts.WebDescription("Select the centering for the web part."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(Orientations.left)
        ]
        public Orientations Orientation
        {
            get
            {
                return orientation;
            }
            set
            {
                orientation = value;
            }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            List<EditorPart> editorParts = new List<EditorPart>(1);
            EditorPart part = new LoginEditorPart();
            part.Title = "Login Part Settings";
            part.ID = this.ID + "_LoginEditorPart";
            editorParts.Add(part);
            EditorPartCollection baseparts = base.CreateEditorParts();
            return new EditorPartCollection(baseparts, editorParts);
        }

        protected override void CreateChildControls()
        {
            try
            {
                StringBuilder script = new StringBuilder();

                string size = "size=\"" + ButtonSize + "\"";
                string length = "";
                if (ButtonLength == Lengths.Shorter)
                    length = "length=\"short\"";
                else if (ButtonLength == Lengths.Longer)
                    length = "length=\"long\"";

                string autologout = "autologoutlink=\"" + AutoLogoutLink.ToString() + "\"";

                script.Append("<table style='padding:5px' width='100%'><tr><td width='100%'  align='" + Orientation.ToString() + "'><fb:login-button " + size + " " + length + " " + autologout + "></fb:login-button></td></tr></table>");

                this.Controls.Add(new LiteralControl(script.ToString()));
            }
            catch (Exception e) { this.Controls.Add(new LiteralControl(e.Message)); }
        }
    }
}
